//
//  JZHMyHostViewController.m
//  极致荟
//
//  Created by hetao on 14-5-24.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHMyHostViewController.h"
#import "JZHPartyCell.h"
#import "JZHMyCollectTableViewCell.h"
#import "JZHShare.h"
#import "JZHHouViewController.h"
#import "JZHActivities.h"
#import "OtherUsers.h"
@interface JZHMyHostViewController ()
{
    NSMutableArray *mydata;
    NSMutableArray *bmdata;
    NSString *imgPrefix;
}
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation JZHMyHostViewController
int tabletype;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    tabletype=1;
    mydata=[[NSMutableArray alloc]init];
    bmdata=[[NSMutableArray alloc]init];
    [self myHttpLoadData];
    if(OSVersionIsAtLeastiOS7>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    self.navigationItem.title=@"我的活动";
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)leftDrawerButtonPress:(UIButton *)but
{
    [UINavigationController Pop:self];
}
#pragma mark ---tableview代理---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tabletype==1)
    {
        return mydata.count;
    }else
    {
        return bmdata.count;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tabletype==1)
    {
        return 383;
    }else
    {
        return 110;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str=@"adf";
    if(tabletype==1)
    {
        JZHPartyCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
        if(!cell)
        {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"JZHPartyCell" owner:self options:nil] objectAtIndex:0];
            
        }
        cell.pusshare=^(void){
            JZHShare *share=[[NSBundle mainBundle]loadNibNamed:@"JZHShare" owner:self options:nil][0];
            [share show:1 viewcontroller:self];
            return share;
        };
        cell.controller=self;
        if(mydata.count!=0)
        {
            [cell adddata:mydata[indexPath.row] headpath:imgPrefix];
        }
        return cell;
    }else
    {
        JZHMyCollectTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
        if(!cell)
        {
            cell=[[NSBundle mainBundle] loadNibNamed:@"JZHMyCollectTableViewCell" owner:self options:nil][0];
            
        }
        [cell addCellData:bmdata[indexPath.row]];
        return cell;

    }
    
}

///我的发起
- (void)myHttpLoadData
{
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/myParty") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            imgPrefix=[[data objectForKey:@"data"] objectForKey:@"imgPrefix"];
            NSArray *arr=[[data objectForKey:@"data"] objectForKey:@"list"];
            for(NSDictionary *datadic in arr)
            {
                //创建活动实体类
                JZHActivities *jzh=[[JZHActivities alloc]init];
                //创建其他用户实体lei
                OtherUsers *ouser=[[OtherUsers alloc]init];
                ouser.userId=[[datadic objectForKey:@"userId"] intValue];
                ouser.userHeadImage=[imgPrefix stringByAppendingString:[datadic objectForKey:@"avatar"]];
                ouser.name=[datadic objectForKey:@"userName"];
                
                jzh.actId=[[datadic objectForKey:@"id"] intValue];
                jzh.addes=[datadic objectForKey:@"address"];
                jzh.actdate=[[NSDate alloc]initWithTimeIntervalSince1970:[[datadic objectForKey:@"partyTime"] longLongValue]];
                jzh.actType=[[datadic objectForKey:@"partyType"] intValue];
                jzh.consumptionMode=[[datadic objectForKey:@"payment"] intValue];
                jzh.acttile=[datadic objectForKey:@"subject"];
                jzh.content=[datadic objectForKey:@"detail"];
                jzh.actimages=[datadic objectForKey:@"partyImage"];
                jzh.mainuser=ouser;
                NSMutableArray *userarr=[[NSMutableArray alloc]init];
               
                for(NSDictionary *userdic in [datadic objectForKey:@"joinList"])
                {
                    if(userdic)
                    {
                     [userarr addObject:[userdic objectForKey:@"userAvatar"]];
                    }
                   
                }
                jzh.userarr=userarr;
                // NSLog(@"%@",jzh.actdate);
                [mydata addObject:jzh];
            
        }
            [_table reloadData];
    }
    }];
}

///我的报名活动
- (void)boHttpLoadData
{
    
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/myPartyJoin") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            for(NSDictionary *datadic in [[data objectForKey:@"data"] objectForKey:@"list"])
            {
                JZHActivities *jzh=[[JZHActivities alloc]init];
                jzh.actdate=[NSDate dateWithTimeIntervalSince1970:[[datadic objectForKey:@"addTime"] longLongValue]];
                jzh.acttile=[datadic objectForKey:@"subject"];
                jzh.actimage=[[[data objectForKey:@"data"] objectForKey:@"imgPrefix"] stringByAppendingString:[datadic objectForKey:@"image"][0]];
                [bmdata addObject:jzh];
            }
            [_table reloadData];
            
        }
    }];
}
- (IBAction)myf:(UIButton *)sender
{
    tabletype=1;
    [sender setBackgroundImage:[UIImage imageNamed:@"activity_detailfield_bg"] forState:UIControlStateNormal];
    [self.but2 setBackgroundImage:nil forState:UIControlStateNormal];
    if(mydata.count<=0)
    {
        [self myHttpLoadData];
    }else
    {
        [self.table reloadData];
    }
    
    
}
- (IBAction)myc:(UIButton *)sender
{
    tabletype=2;
    [sender setBackgroundImage:[UIImage imageNamed:@"activity_detailfield_bg"] forState:UIControlStateNormal];
    [self.but1 setBackgroundImage:nil forState:UIControlStateNormal];
    if(bmdata.count<=0)
    {
        [self boHttpLoadData];
    }else
    {
        [self.table reloadData];
    }
   
    
}


@end
